<?php
class user {

    private $registry;
    var $userData;
    private $db;
    
    function __construct($reg) {
        $this->registry = $reg;
        $this->db = $this->registry->get('db');
    }

    function Login($login,$pass) {
        $this->userData = $this->db->getUserDataByLogin($login);
        if(!$this->userData) {
            return 'Error: User not found';
        }

        $pass = md5(md5($pass).md5($this->userData['salt']).'metacp');

        if($pass === $this->userData['pass']) {
            $_SESSION['uid'] = $this->userData['id'];
            $session_id = md5($_SERVER['REMOTE_ADDR'].$_SESSION['uid']);
            setcookie('meta_session',$session_id,time()+604800);
            $this->db->sessionRegister($session_id,$_SESSION['uid']);
        }
        else {
            return 'Error: Wrong Password';
        }
        return '';

    }

    function autoLogin() {
        if(!isset($_SESSION['uid'])) {
            $sid = $_COOKIE['meta_session'];
            if(preg_match('/^[a-zA-Z0-9]+$/',$sid) != 0 ) {
                $this->userData = $this->db->getUserDataBySID($sid);
                $_SESSION['uid'] = $this->userData['id'];
            }
        }
        else {
            $this->userData = $this->db->getUserDataByID($_SESSION['uid']);
        }
    }


    function logout() {
        unset($_SESSION['uid']);
        $sid = $_COOKIE['meta_session'];

        if(preg_match('/^[a-zA-Z0-9]+$/',$sid) == 0 ) {
            $sid = '';
        }
        
        $this->db->sessionUnregister($sid);
        setcookie('meta_session','', time() - 3600);
        header("Location: {$config['misc']['site_url']}?p=index");
        echo "K.O.";
    }


    function getAccess() {
        return $this->userData['access'];
    }

    function sendActivationMail($login,$mail) {

        $tp = $this->registry->get('template');

        $tp->set('login', $login);
        $linkid = md5(time().$_SERVER['REMOTE_ADDR']);

        $this->db->addActivationCode($linkid,$login);

        $link = site_url.'?p=user/register/confirm/'.$linkid;
        $tp->set('link', $link);

        $msg_body = $tp->apply_sys('activate_acc');
        return sendMail($mail,'Account activation',$msg_body);
    }

    function sendPassActivationMail($login,$mail) {

        $tp = $this->registry->get('template');

        $tp->set('login', $login);
        $linkid = md5(time().$_SERVER['REMOTE_ADDR']);

        $this->db->addActivationCode($linkid,$login);

        $link = site_url.'?p=user/restorepassword/confirm'.$linkid;
        $tp->set('link', $link);

        $msg_body = $tp->apply_sys('activate_pass');
        return sendMail($mail,'Account activation',$msg_body);
    }

    function doRegister($login,$mail,$pass) {
        
        $error = true;
        $error_str = '';

        $error = $this->db->CheckLogin($login);
        if(!$error) {
            $error_str .= "That name already taken ! <br /><br />";
        }
        $error = $this->db->CheckMail($mail);
        if(!$error) {
            $error_str .= "That e-mail already taken !<br /><br />";
        }
        if(!empty($error_str)) {
            return $error_str;
        }

        $salt = genRndString(5);
        $pass = md5(md5($pass).md5($salt).'metacp');
        $date = time();

        $this->db->CreateUser($login, $pass, $mail, $salt, $date);
        $this->sendActivationMail($login, $mail);

    }

    function activateReg($code) {
        $db = $this->registry->get('db');
        $uid = $db->getUserIDByCode($code);

        if($uid != 0) {
            $db->ActivateUser($uid, $code);
            $_SESSION['uid'] = $uid;
            return TRUE;
        }
        else {
            return FALSE;
        }

    }
}
?>
